//
//  HWMyWalletViewController.m
//  嗨文
//
//  Created by 响当当 on 2019/2/19.
//  Copyright © 2019年 dongzheng. All rights reserved.
//

#import "HWMyWalletViewController.h"
#import "HWMyWalletView.h"
#import "HWMyWalletModel.h"

@interface HWMyWalletViewController ()
@property (nonatomic, strong)HWMyWalletView *myWalletView;
@end

@implementation HWMyWalletViewController

- (HWMyWalletView *)myWalletView
{
    if (!_myWalletView)
    {
        _myWalletView = [[HWMyWalletView alloc]initWithFrame:CGRectMake(0,NAVIBAR_HEIGHT,SCREEN_WIDTH,SCREEN_HEIGHT-NAVIBAR_HEIGHT)];
        WeakSelf(self)
        _myWalletView.getBackSegmentChangeBlock = ^(NSString *num) {
            if ([num isEqualToString:@"0"])
            {
                [weakSelf getGoldRecordInfo:[NSString stringWithFormat:@"%ld",num.integerValue+1] page:@"1" pagesize:@"10"];
            }
            else
            {
                [weakSelf getGoldExchangeAndMoneyInfo:num page:@"1" pagesize:@"10"];
            }
        };
        
        _myWalletView.upLoadIndexBlcok = ^(NSString *page, NSString *size) {
            [weakSelf getGoldRecordInfo:@"1" page:page pagesize:size];
        };
        _myWalletView.goldOrMoneyBlock = ^(NSString *type, NSString *page, NSString *size) {
            [weakSelf getGoldExchangeAndMoneyInfo:type page:page pagesize:size];
        };
    }
    return _myWalletView;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setTitle:@"我的钱包"];
    [self setDefaultNav];
    [self getInfor];
    [self getGoldRecordInfo:@"1" page:@"1" pagesize:@"10"];
    
    [self.view addSubview:self.myWalletView];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self buryPoint];
}

#pragma mark - 获取我的钱包信息数据
- (void)getInfor
{
    
    NSDictionary *prarmers = @{
                               @"access_token" : current_User.token                               };
    
    WeakSelf(self)
    [MyInfoManager getWallet:prarmers sucess:^(id  _Nonnull acceptData) {
        DISMISS_SVP(1.0f);
        HWMyWalletModel *model = [[HWMyWalletModel alloc]initWithDic:acceptData];
        [weakSelf.myWalletView getDate:model];
    } fail:^(NSError * _Nullable error) {
        DISMISS_SVP(1.0f);
        [SVProgressHUD showErrorWithStatus:@"获取我的钱包信息失败，请检查网络是否良好"];
        DISMISS_SVP(1.0f);
    }];
}

#pragma mark - 获取金币获得记录
- (void)getGoldRecordInfo:(NSString *)sourceType page:(NSString *)pageNum pagesize:(NSString *)pagesize
{
    
    NSDictionary *prarmers = @{
                               @"access_token" : current_User.token,
                               @"sourceType"   : sourceType,
                               @"pageNum" :pageNum,
                               @"pageSize" : pagesize
                               };
    
    WeakSelf(self)
    [MyInfoManager getWalletCoinRecord:prarmers sucess:^(id  _Nonnull acceptData) {
        NSNumber *nub = IFNULL(acceptData[@"hasNextPage"], [NSNumber new]);
        NSArray *arr = [NSArray arrayWithArray:acceptData[@"list"]];
        [weakSelf.myWalletView getGoldRecord:arr hasNextPage:nub];
    } fail:^(NSError * _Nullable error) {
        DISMISS_SVP(1.0f);
        [SVProgressHUD showErrorWithStatus:@"获取我的钱包信息失败，请检查网络是否良好"];
        DISMISS_SVP(1.0f);
    }];
}

#pragma mark - 获取金币兑换记录 和余额兑现记录
- (void)getGoldExchangeAndMoneyInfo:(NSString *)type page:(NSString *)pageNum pagesize:(NSString *)pagesize
{
    
    NSDictionary *prarmers = @{
                               @"access_token" : current_User.token,
                               @"pageNum" :pageNum,
                               @"pageSize" : pagesize
                               };
    WeakSelf(self)
    if ([type isEqualToString:@"1"])
    {
        [MyInfoManager setGoldenExChangeInfo:prarmers sucess:^(id  _Nonnull acceptData) {
            NSNumber *nub = IFNULL(acceptData[@"hasNextPage"], [NSNumber new]);
            NSArray *arr = [NSArray arrayWithArray:acceptData[@"list"]];
            [weakSelf.myWalletView getGoldExChange:arr hasNextPage:nub];
        } fail:^(NSError * _Nullable error) {
            DISMISS_SVP(1.0f);
            [SVProgressHUD showErrorWithStatus:@"获取信息失败，请检查网络是否良好"];
            DISMISS_SVP(1.0f);
        }];
    }
    else
    {
        [MyInfoManager setMoneyExChangeInfo:prarmers sucess:^(id  _Nonnull acceptData) {
            NSNumber *nub = IFNULL(acceptData[@"hasNextPage"], [NSNumber new]);
            NSArray *arr = [NSArray arrayWithArray:acceptData[@"list"]];
            [weakSelf.myWalletView getMoneyExChange:arr hasNextPage:nub];
        } fail:^(NSError * _Nullable error) {
            DISMISS_SVP(1.0f);
            [SVProgressHUD showErrorWithStatus:@"获取信息失败，请检查网络是否良好"];
            DISMISS_SVP(1.0f);
        }];
    }
}

- (void)buryPoint
{
    NSString *lastStayTime = [kUserDefaults objectForKey:@"time"];
    NSString *nowStayTime = [NSString stringWithFormat:@"%ld",[[HWUtilties getNowTimeTimestamp3] integerValue] - lastStayTime.integerValue];
    
    [HWUtilties buryPointWithStayTime:nowStayTime
                            timestamp:[HWUtilties getNowTimeTimestamp3]
                             memberId:current_User.userID
                              fromUrl:nil
                                toUrl:@"/myWallet"
                               fromId:nil
                                 toId:@"f22470101c25e23f"];
}

@end
